package com.csdj.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.csdj.server.mapper.MenuRoleMapper;
import com.csdj.server.pojo.MenuRole;
import com.csdj.server.pojo.RespBean;
import com.csdj.server.service.IMenuRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zyq
 * @since 2022-12-12
 */
@Service
public class MenuRoleServiceImpl extends ServiceImpl<MenuRoleMapper, MenuRole> implements IMenuRoleService {

    @Autowired(required = false)
    private MenuRoleMapper menuRoleMapper;
    /**
     * 更新菜单角色
     * @param rid
     * @param mids
     * @return
     */
    @Override
    @Transactional //加入事务 操作
    public RespBean updateMenuRole(Integer rid, Integer[] mids) {
        //1.先根据rid角色Id 清除角色菜单信息
        menuRoleMapper.delete(new QueryWrapper<MenuRole>()
                .eq("rid",rid));
        if(mids==null||mids.length==0){
            return RespBean.success("更新成功!");
        }
        //2
        Integer record = menuRoleMapper.insertRecord(rid, mids);

        //判断受影响行数 和数组的长度是否一致
        if(mids.length==record){
           return RespBean.success("更新成功!");
        }

        return RespBean.error("更新失败!");
    }
}
